import * as React from "react";
import { Provider } from "react-redux";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import layoutCreater from "../layout";
import genisisCreater from "../container/genisis";
// import "./app.less";
import isNil from "lodash/isNil";

interface AppProps {
  store: any;
}

export default async options => {
  const [components, Layout] = await Promise.all([
      genisisCreater(options),
      layoutCreater(options)
  ]);
  class App extends React.Component<any, any> {
    public props: AppProps;
    constructor(props) {
      super(props);
      this.state = {
        defaultComponent: components.login
        // defaultComponent: { exact: true, path: "/login", component: undefined }
      };
    }/*
    public processing() {
      this.setState({
        defaultComponent: components.login
      });
    }

    public componentDidMount() {
      this.processing();
    }*/

    public render() {
      const { defaultComponent } = this.state;
      const { component } = defaultComponent;
      const store = this.props.store;
      const { session } = store.getState();
      return (
        <Provider store={store}>
          <Router>
            <Switch>
              <Route {...defaultComponent} />
              {isNil(session) ? null : (
                <Route path="/dashboard" component={Layout} />
              )}
              <Route path="/*" component={component} />
            </Switch>
          </Router>
        </Provider>
      );
    }
  }
  return App;
};
